﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.IBLL;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.Web.Tests.Fakes
{
    public class FakeIterationManager : IIterationManager   
    {
        private List<Iteration> iterationList;

        public FakeIterationManager(List<Iteration> iterations)
        {
            iterationList = iterations;
        }

        /// <summary>
        /// Create a iteration (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public Iteration NewIteration()
        {
            var iteration = new Iteration();

            iteration.CreateTime = DateTime.Now;
            iteration.Name = "New Iteration"; // hard coded default Iteration text
            iteration.PeriodFrom = DateTime.Now;
            iteration.PeriodTo = DateTime.Now.AddDays(13);            

            return iteration;
        }

        /// <summary>
        /// Save iteration
        /// </summary>
        /// <param name="iteration">Iteration entity</param>
        /// <returns></returns>
        public Iteration Save(Iteration iteration)
        {
            Validate(iteration);
            if (Get(iteration.IterationID) != null)
                throw new Exception("Existing iteration");
            iteration.IterationID = Guid.NewGuid();
            iterationList.Add(iteration);

            return Get(iteration.IterationID);
        }

        /// <summary>
        /// Access to iterations
        /// </summary>
        /// <param name="iterationId">Iteration ID</param>
        /// <returns></returns>
        public Iteration Get(Guid iterationId)
        {
            return iterationList.SingleOrDefault(u => u.IterationID == iterationId);
        }

        /// <summary>
        /// Access to iterations
        /// </summary>
        /// <param name="iterationId">Iteration ID</param>
        /// <returns></returns>
        public IList GetByRelease(Guid releaseId)
        {
            return iterationList.FindAll(r => r.ReleaseID == releaseId);
        }      

        /// <summary>
        /// Delete Iteration
        /// </summary>
        /// <param name="iterationId">Iteration ID</param>
        /// <returns></returns>
        public void Delete(Guid iterationId)
        {
            var iteration = Get(iterationId);
            if (iteration == null) throw new Exception("NotFound");
            iterationList.Remove(iteration);
        }

        /// <summary>
        /// Update Iteration
        /// </summary>
        /// <param name="iteration">Iteration Entity</param>
        /// <returns></returns>
        public Iteration Update(Iteration iteration)
        {
            return Get(iteration.IterationID);
        }

        /// <summary>
        /// Access to iterations
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return iterationList;
        }

        private void Validate(Iteration iteration)
        {
            //TODO throw exception on error.
        }        
    }
}
